import { query, addRole } from '../../services/system/permission';

export default {
  namespace: 'permission',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    currentUser: {},
  },

  effects: {
    *fetch(_, { call, put }) {
      const { data } = yield call(query, {});
      data.list = data.list.map(item => {
        return {
          key: item.id,
          ...item,
        };
      });
      yield put({
        type: 'save',
        payload: data,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const { code } = yield call(addRole, { ...payload });
      if (code === 0) {
        const { data } = yield call(query, {});
        yield put({
          type: 'save',
          payload: data,
        });
      }
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
